Returns the names and paths to files of specified types from a directory and optionally its subdirectories as well.

=======================================================

<?php
/**
 * GetFiles
 *
 * Reads a specified directory[ and its sub
 * directories (optional)] searching for files
 * [ of the allowed file types specified in the
 * third parameter (optional) lowercased and without
 * dots (png, php, gif, etc...)].
 *
 * To make the function return all files within the
 * directory[ and its sub directories (optional)]
 * include * in allowed types.
 *
 * If the allowed types array contains * all
 * additional elements will be assumed as disallowed
 * types and prevent the function from returning
 * files of such type.
 *
 * Allowed types defaults to * and are therefore
 * optional.
 *
 * To make the function travers subdirectories set
 * second (optional) parameter to true, defaults
 * to false.
 *
 * @author	Niklas Kllander <niklas@wimpy.se>
 *
 * @param	string	$dir_path	the directory to read
 * @param	bool	$search_deep	determines whether or not to read sub directories of the specified directory or not
 * @param	array	$allowed_types	the allowed types to return or the disallowed types that shouldn't be returned
 * @return	array			an array containing the filenames( the element's value), and the full paths including the filename( the element's key).
 */
function GetFiles($dir_path, $search_deep = false, array $allowed_types = Array('*'))
{
	// Check if provided string is a directory and if
	// the number of allowed file types is at least 1,
	// if not, throws an exception.
	if( ! is_dir($dir_path) || count($allowed_types) == 0)
	{
		throw new Exception('No such directory( '.$dir_path.' ) or insufficient number of allowed types!');
	}

	// Explicitly convert the flag to a boolean.
	$search_deep = (bool)$search_deep;

	// If the client has specified '*' in the allowed types
	// all other types are assumed to be disallowed types.
	$disallowed_types = Array();
	if(in_array('*', $allowed_types))
	{
		foreach($allowed_types as $type)
		{
			if($type != '*')
			{
				$disallowed_types[] = $type;
			}
		}
	}

	// An array which will hold all the found files.
	$_files = Array();

	// Open the specified directory.
	$dir = opendir($dir_path);

	// Continue Looping until we've iterated through the whole directory.
	while (false !== ($file = readdir($dir)))
	{
		// '.' and '..' represents the current directory and
		// the parent directory, therefore we ignore those.
		if($file != '.' && $file != '..')
		{
			// If the current file is a sub directory, and if the
			// optional flag is set to true, we traverse it.
			$full_filename = $dir_path."/".$file;
			if(is_dir($full_filename) && $search_deep)
			{
				// Merging the return value with the array of files to return.
				$_files = array_merge($_files, GetFiles($full_filename, $search_deep, $allowed_types));
			}
			else if(is_file($full_filename))
			{
				// Finds and lowercases the file extension.
				$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

				// If the type of the file is allowed, we add
				// it to the array of files to return.
				if((in_array($ext, $allowed_types) || in_array('*', $allowed_types)) && ! in_array($ext, $disallowed_types))
				{
					// Set the full filename including path as key
					// and the short filename as value.
					$_files[$full_filename] = $file;
				}
			}
		}
	}
	// Closes the directory and returns the $_files-array
	closedir($dir);
	return $_files;
}
?>

<?php
// An example of its usage.
try
{
	// Searches the directory, and its sub directories for files
	// that are not of the type 'php'.
	$files = GetFiles('path/to/directory', true, Array('*', 'php'));
	foreach($files as $filepath => $file)
	{
		echo $filepath." => ".$file."<br />";
	}
}
catch(Exception $e)
{
	echo $e->getMessage();
}
?>